<template>
	<view>
		<view class="content">
			
			<view class="row">
				<view class="nominal">
					姓名
				</view>
				<view class="input">
					<text v-if="userinfo.user_name">{{userinfo.user_name}}</text>
					<input v-else placeholder="请输入姓名" type="text" v-model="form.user_name" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					支付宝账号
				</view>
				<view class="input">
					<text v-if="userinfo.alipay_account">{{userinfo.alipay_account}}</text>
					<input v-else placeholder="请输入支付宝账号" type="text" v-model="form.alipay_account" />
				</view>
			</view>
			<!-- <view class="row">
				<view class="nominal">
					电话号码
				</view>
				<view class="input">
					<input placeholder="请输入收件人电话号码" type="text" v-model="tel" />
				</view>
			</view> -->
			<view class="row">
				<view class="nominal">
					银行名称
				</view>
				<!-- <view class="input" @tap="chooseCity">
					{{region.label}}
				</view> -->
				<view class="input">
					<text v-if="userinfo.backname">{{userinfo.backname}}</text>
					<picker v-else mode="selector" @change="bindPickerChange" :value="index" :range="array">
						<view class="uni-input" style="align-items: center;">{{array[index]}} <uni-icons type="forward" size="15"></uni-icons></view>
					</picker>
				</view>
				
				
			</view>
			<view class="row">
				<view class="nominal">
					银行卡号
				</view>
				<view class="input">
					<text v-if="userinfo.bankaccount">{{userinfo.bankaccount}}</text>
					<textarea v-else v-model="form.bankaccount" auto-height="true" placeholder="输入银行卡号"></textarea>
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					USDT
				</view>
				<view class="input">
					<text v-if="userinfo.ETHaddress">{{userinfo.ETHaddress}}</text>
					<textarea v-else v-model="form.ETHaddress" auto-height="true" placeholder="输入USDT信息"></textarea>
				</view>
			</view>
			<!-- <view class="row">
				<view class="nominal">
					设置默认地址
				</view>
				<view class="input switch">
					<switch color="#f06c7a" :checked="isDefault" @change=isDefaultChange />
				</view>
			</view> -->
			<view class="row">
				<view class="del">
					温馨提示：支付资料一经提交无法修改，请核对无误后提交资料。
				</view>
			</view>
		</view>
		
		<view class="save disabled" @tap="$api.msg('已提交资料不可编辑')"  v-if="userinfo.alipay_account">
			<view class="btn">
				已提交
			</view>
		</view>
		
		<view class="save" @tap="save" v-else>
			<view class="btn">
				提交资料
			</view>
		</view>
		
		
		<mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValue" @onCancel="onCancel" @onConfirm="onConfirm"></mpvue-city-picker>
	</view>
</template>

<script>
	import service from '@/service.js';
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue'
	export default {
		components: {
			mpvueCityPicker
		},
		data() {
			return {
				userinfo:{},
				editType:'edit',
				form:{
					alipay_account:'',//支付宝账号
					backname:'',//银行名称
					bankaccount:'',//银行卡号
					user_name:'',
					ETHaddress:'',//usdt地址
					
				},
				id:'',
				name:'',
				tel:'',
				detailed:'',
				isDefault:false,
				cityPickerValue: [0, 0, 1],
				themeColor: '#007AFF',
				region:{label:"请点击选择银行",value:[],cityCode:""},
				array:["请选择","中国工商银行","中国农业银行","中国银行","中国建设银行","交通银行","中信银行","中国光大银行","华夏银行","中国民生银行","广发银行","兴业银行","上海浦东发展银行","平安银行","中国邮政储蓄银行","莱商银行","天津银行","上海银行","重庆银行","包商银行","盛京银行","大连银行","哈尔滨银行","南京银行","杭州银行","宁波银行","浙江民泰商业银行","泉州银行","江西银行","齐鲁银行","青岛银行","临商银行","齐商银行","汉口银行","广州银行","东莞银行","乌鲁木齐银行","浙商银行","渤海银行","北京农商银行","上海农商银行","兰州银行","青海银行","甘肃银行","贵州银行","锦州银行","日照银行","河北银行","潍坊银行","威海市商业银行","珠海华润银行","贵阳银行","宁夏银行","无锡农商行","广州农商银行","众邦银行"],
				index:0
			};
		},
		async onShow() {
			var result = (await this.$ajax.get('users/getuserinfo.html'));
			if (result.data.code == 1) {
				service.updateUser(result.data.data);
				// setTimeout(function() {
				// uni.switchTab({
				// 	url: '/pages/tabBar/user/user',
				// })
				// }, 1000);
			}
			this.userinfo=uni.getStorageSync("USERS_KEY");
		},
		methods: {
			bindPickerChange(e){
				console.log('picker发送选择改变，携带值为', e.detail.value);
				this.index = e.detail.value;
				this.form.backname = this.array[this.index];
			},
			onCancel(e) {
				console.log(e)
			},
			chooseCity() {
				this.$refs.mpvueCityPicker.show()
			},
			onConfirm(e) {
				this.region = e;
				this.cityPickerValue = e.value;
			},
			isDefaultChange(e){
				this.isDefault = e.detail.value;
			},
			del(){
				uni.showModal({
					title: '删除提示',
					content: '你将删除这个收货地址',
					success: (res)=>{
						if (res.confirm) {
							this.deladdress();
							uni.setStorage({
								key:'delAddress',
								data:{id:this.id},
								success() {
									uni.navigateBack();
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				
			},
			async deladdress(){
				let results = (await this.$ajax.get('usersaddress/delAddress.html?id='+this.id));
				this.$api.msg(results.data.msg);
			},
			async save(){
				// let data={"name":this.name,"head":this.name.substr(0,1),"tel":this.tel,"address":{"region":this.region,"detailed":this.detailed},"isDefault":this.isDefault}
				// if(this.editType=='edit'){
				// 	data.id = this.id
				// }
				if(!this.form.user_name){
					uni.showToast({title:'请输入姓名',icon:'none'});
					return ;
				}
				if(!this.form.alipay_account){
					uni.showToast({title:'请输入支付宝账号',icon:'none'});
					return ;
				}
				if(this.index==0){
					uni.showToast({title:'请选择银行名称',icon:'none'});
					return ;
				}
				if(!this.form.bankaccount){
					uni.showToast({title:'请输入银行卡号',icon:'none'});
					return ;
				}
				if(!this.form.ETHaddress){
					uni.showToast({title:'请输入USDT信息',icon:'none'});
					return ;
				}
				uni.showLoading({
					title:'正在提交'
				})
				
				let results = (await this.$ajax.get('users/setpayinfo.html',{data:this.form}));
				//实际应用中请提交ajax,模板定时器模拟提交效果
				console.log(results);
				
				this.$api.msg(results.data.msg);
				if(results.data.code==1){
					setTimeout(()=>{
						uni.switchTab({
							url:'/pages/tabBar/user/user'
						})
						// uni.setStorage({
						// 	key:'saveAddress',
						// 	data:data,
						// 	success() {
						// 		uni.hideLoading();
						// 		uni.navigateBack();
						// 	}
						// })
						
					},800)
				}
				// setTimeout(()=>{
					// uni.setStorage({
					// 	key:'saveAddress',
					// 	data:data,
					// 	success() {
					// 		uni.hideLoading();
					// 		uni.navigateBack();
					// 	}
					// })
					
				// },800)
				
				
			},
			async getuserinfo(){
				var result = (await this.$ajax.get('users/getuserinfo.html'));
				if (result.data.code == 1) {
					service.updateUser(result.data.data);
					setTimeout(function() {
					uni.switchTab({
						url: '/pages/tabBar/user/user',
					})
					}, 1000);
				
				}
			}
		},
		async onLoad(e) {
			//获取传递过来的参数
			
			this.editType = e.type;
			if(e.type=='edit'){
				uni.getStorage({
					key:'address',
					success: (e) => {
						this.id = e.data.id;
						this.name = e.data.name;
						this.tel = e.data.tel;
						this.detailed = e.data.address.detailed;
						this.isDefault = e.data.isdefault;
						this.cityPickerValue = e.data.address.region.value;
						this.region = e.data.address.region;
					}
				})
			}
			
		},
		onBackPress() {
			if (this.$refs.mpvueCityPicker.showPicker) {
				this.$refs.mpvueCityPicker.pickerCancel();
				return true;
			}
		},
		onUnload() {
			
			if (this.$refs.mpvueCityPicker.showPicker) {
				this.$refs.mpvueCityPicker.pickerCancel()
			}
		}
	};
</script>
<style lang="scss">

.save{
		view{
			display: flex;
		}
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		display: flex;
		justify-content: center;
		align-items: center;
		.btn{
			box-shadow: 0upx 5upx 10upx rgba(0,0,0,0.4);
			width: 70%;
			height: 80upx;
			border-radius: 80upx;
			background-color: #f06c7a;
			color: #fff;
			justify-content: center;
			align-items: center;
			.icon{
				height: 80upx;
				color: #fff;
				font-size: 30upx;
				justify-content: center;
				align-items: center;
			}
			font-size: 30upx;
		}
	}
	.disabled{
		.btn{
			background-color: $uni-border-color;
		}
		
	}
	.content{
		display: flex;
		flex-wrap: wrap;
		view{
			display: flex;
		}
		.row{
			width: 94%;
			
			margin: 0 3%;
			border-top: solid 1upx #eee;
			.nominal{
				width: 30%;
				height: 120upx;
				font-weight: 200;
				font-size: 30upx;
				align-items: center;
			}
			.input{
				width: 70%;
				padding: 20upx 0;
				align-items: center;
				font-size: 30upx;
				&.switch{
					justify-content: flex-end;
				}
				.textarea{
					margin: 20upx 0;
					min-height: 120upx;
				}
			}
			.del{
				width: 100%;
				// height: 100upx;
				padding:25rpx;
				justify-content: center;
				align-items: center;
				font-size: 36upx;
				color: #f06c7a;
				background-color: rgba(255,0,0,0.05);
				border-bottom: solid 1upx #eee;
			}
		}
	}
	
</style>
